package com.iqiyi.pps.epg.web.webapp.action.reservation;

import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.model.channel.ChannelConfig;
import com.iqiyi.pps.epg.core.model.control.LocationControl;
import com.iqiyi.pps.epg.core.model.control.PlatformControl;
import com.iqiyi.pps.epg.core.model.reservation.ReserveControl;
import com.iqiyi.pps.epg.core.service.broadcast.BroadcastControlService;
import com.iqiyi.pps.epg.core.service.channel.ChannelService;
import com.iqiyi.pps.epg.core.service.reservation.ReservationService;
import com.iqiyi.pps.epg.web.utils.Struts2Utils;
import com.iqiyi.pps.epg.web.webapp.action.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;

import java.util.*;

@Namespace("/reservation/reservation")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Results({@Result(name = BaseAction.RELOAD, location = "reservation.action", type = "redirect")})
public class ReservationAction extends BaseAction<ReserveControl> {
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private BroadcastControlService broadcastControlService;
    @Autowired
    private ChannelService channelService;

    private Page<ReserveControl> page;
    private ReserveControl entity;
    private Long id;
    private Map<String, String> platformConfigs = new HashMap();
    private Map<String, String> locationConfigs = new HashMap();
    private List<Map<String, String>> channelPlatformConfigs;
    private String apiHost = Configuration.getString("local.api.host");

    public Map<String, String> getPlatformConfigs() {
        return platformConfigs;
    }

    public Map<String, String> getLocationConfigs() {
        return locationConfigs;
    }

    public List<Map<String, String>> getChannelPlatformConfigs() {
        return channelPlatformConfigs;
    }

    public String getApiHost() {
        return apiHost;
    }

    @Override
    @Action(value = "list")
    public String list() throws Exception {
        List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
        if (!page.isOrderBySetted()) {
            page.setOrder("id");
            page.setOrderBy(Page.DESC);
            page.setPageSize(20);
        }
        // 平台播控配置
        List<ChannelConfig> platforms = channelService.getConfig(ChannelConfig.PLAY_PLATFORM);
        for (ChannelConfig item : platforms) {
            platformConfigs.put(item.getKey().toString(), item.getVal());
        }
        page = reservationService.get(page, filters);
        return SUCCESS;
    }

    @Override
    @Action(value = "input")
    public String input() throws Exception {

        long channelId = 0;
        if (StringUtils.isNotEmpty(Struts2Utils.getParameter("fatherId"))) {
            channelId = Long.valueOf(Struts2Utils.getParameter("fatherId"));
        }

        Set<Integer> channelPlatformSet = new HashSet();
        Set<Integer> channelLocationSet = new HashSet();
        if (channelId > 0) {
            List<PlatformControl> channelPlatformList = broadcastControlService.getPlatformControls(channelId);
            for (PlatformControl item : channelPlatformList) {
                channelPlatformSet.add(item.getPlayPlatform());
            }
            List<LocationControl> channelLocationList = broadcastControlService.getLocationControls(channelId);
            for (LocationControl item : channelLocationList) {
                channelLocationSet.add(item.getLocation());
            }
        }
        // 平台播控配置
        List<ChannelConfig> platforms = channelService.getConfig(ChannelConfig.PLAY_PLATFORM);
        channelPlatformConfigs = new ArrayList(platforms.size());
        for (ChannelConfig item : platforms) {
            Map<String, String> temp = new HashMap();
            temp.put("key", item.getKey().toString());
            temp.put("val", item.getVal());
            temp.put("checked", channelPlatformSet.contains(item.getKey()) ? "1" : "0");
            channelPlatformConfigs.add(temp);
        }
        // 地区播控
        List<ChannelConfig> locations = channelService.getConfig(ChannelConfig.LOCATION);
        for (ChannelConfig item : locations) {
            locationConfigs.put(item.getKey().toString(), item.getDescr());
        }
        return INPUT;
    }

    @Override
    @Action(value = "save")
    public String save() throws Exception {
        if (entity == null) {
            addActionMessage("保存失败，id为空");
        } else {
            reservationService.save(entity);
            addActionMessage("保存成功");
        }

        return INPUT;
    }

    @Override
    @Action(value = "delete")
    public String delete() throws Exception {
        if (id == null) {
            code = "A00001";
            message = "id为空";
        } else {
            reservationService.del(id);
            code = "A00000";
            message = "删除成功";
        }
        renderAjaxMessage(code, message);
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = reservationService.getById(id);
            if (entity == null) {
                entity = new ReserveControl();
            }
        } else {
            entity = new ReserveControl();
        }
    }

    @Override
    public ReserveControl getModel() {
        return entity;
    }

    public Page<ReserveControl> getPage() {
        return page;
    }

    public void setPage(Page<ReserveControl> page) {
        this.page = page;
    }

    public ReserveControl getEntity() {
        return entity;
    }

    public void setEntity(ReserveControl entity) {
        this.entity = entity;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
